package intro.databases.gtCarRental.ui.employee;

import intro.databases.gtCarRental.GTCRActivity;
import intro.databases.gtCarRental.R;
import intro.databases.gtCarRental.classes.Car;
import intro.databases.gtCarRental.classes.Location;
import intro.databases.gtCarRental.classes.MaintenanceRequest;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class MaintenanceRequests extends GTCRActivity {
	MaintenanceRequest request;
	
	Spinner location, car;
	EditText problem;

	ArrayList<Location> locs;
	ArrayList<Car> cars;
	int carIndex = 0;

	@Override
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.maint_request);
		setupViews();
	}

	private void setupViews() {
		location = (Spinner) findViewById(R.id.spinner_maintenanceRequest_location);
		car = (Spinner) findViewById(R.id.spinner_maintenanceRequest_car);

		problem = (EditText) findViewById(R.id.editText_maintenanceRequest_problem);

		locs = dbConn.getAllLocations();
		try {
			cars = dbConn.getCarsByLocation(locs.get(0).locationName);
			request = dbConn.getMaintenanceRequestForCarSNO(cars.get(0).vehicleSNO, application.currentUser.username);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		carIndex = 0;

		ArrayAdapter<String> lcAdapter = getLocationAdapter();
		lcAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		location.setAdapter(lcAdapter);
		location.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				cars = dbConn.getCarsByLocation(locs.get(arg2).locationName);
				ArrayAdapter<String> cAdapter = getCarAdapter();
				cAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				car.setAdapter(cAdapter);
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		});

		ArrayAdapter<String> cAdapter = getCarAdapter();
		cAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		car.setAdapter(cAdapter);
		car.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				try {
					carIndex = arg2;
					request = dbConn.getMaintenanceRequestForCarSNO(cars.get(carIndex).vehicleSNO, application.currentUser.username);
					problem.setText(request.problems);
				} catch (Exception e) {
					//if there are no cars at the location
				}
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		});
		
		
		problem.setText(request.problems);
	}



	private ArrayAdapter<String> getLocationAdapter() {
		ArrayList<String> locNames = new ArrayList<String>();
		for(Location loc : locs)
			locNames.add(loc.locationName);

		ArrayAdapter<String> retVal = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,locNames);		
		return retVal;		
	}



	private ArrayAdapter<String> getCarAdapter() {
		ArrayList<String> carNames = new ArrayList<String>();
		try {
			for(Car car : cars)
				carNames.add(car.carModel);
		} catch (Exception e) {
			e.printStackTrace();
		}

		ArrayAdapter<String> retVal = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, carNames);		
		return retVal;		
	}

	
	public void submit(View v) {
		request.problems = problem.getText().toString();
		request.username = application.currentUser.username;
		
		Car c = cars.get(carIndex);
		c.underMaintenanceFlag = !request.problems.equals("");
		
		dbConn.updateMaintenanceRequestForCar(request, c);
	}
}
